package com.mlk.engSystem.service.impl;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.mlk.constant.EngConstant;
import com.mlk.core.component.page.Page;
import com.mlk.core.component.page.Pager;
import com.mlk.core.constants.Constant;
import com.mlk.core.exception.GlobalException;
import com.mlk.core.util.FileUploadUtils;
import com.mlk.core.util.ServerResponse;
import com.mlk.core.util.SessionUtils;
import com.mlk.core.util.UUIDUtils;
import com.mlk.engSystem.mapper.EngSystemBannerMapper;
import com.mlk.engSystem.po.EngSysBanner;
import com.mlk.engSystem.service.EngSystemBannerService;
import com.mysql.fabric.Server;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 系统banner service
 * @author xulu
 */
@Service
public class EngSystemBannerServiceImpl implements EngSystemBannerService {

    @Resource
    private EngSystemBannerMapper engSystemBannerMapper;

    @Page
    @Override
    public Pager listPage() {
        return Pager.adapt(this.engSystemBannerMapper.selectList(Condition.create().orderAsc(Arrays.asList("seq"))));
    }

    @Transactional
    @Override
    public ServerResponse add(EngSysBanner banner, MultipartFile file) {
        FileUploadUtils.UploadContext context = null;
        String path = banner.getType().equals(1) ? EngConstant.PicPath.SYSTEM_BANNER_CLIENT :EngConstant.PicPath.SYSTEM_BANNER_SERVER ;
        if ((context = FileUploadUtils.uploadContext(file, path,UUIDUtils.uuid())).getSuccess()) {
            banner.setCreateName(SessionUtils.getUser().getSysUser().getName());
            banner.setCreateTime(new Date());
            banner.setUrl(context.getSaveFullPath());
            int count = this.engSystemBannerMapper.insert(banner);

            return count == 0 ? ServerResponse.fail("添加失败") : ServerResponse.ok("添加成功");
        }

        throw new GlobalException("添加失败");
    }


    @Override
    public ServerResponse update(EngSysBanner banner) {
        return this.engSystemBannerMapper.updateById(banner) == 0 ? ServerResponse.fail("操作失败"):ServerResponse.ok("操作成功");
    }

    /**
     * 客户端获取轮播图
     * @return
     */
    @Override
    public ServerResponse apiList() {
        List<EngSysBanner> banners = this.engSystemBannerMapper.selectList(new EntityWrapper<>(EngSysBanner.builder().status(Constant.status_ok).type(1).build()));
        return ServerResponse.ok(banners);
    }

    /**
     * 业务端获取轮播图
     * @return
     */
    @Override
    public ServerResponse listBanner() {
        List<EngSysBanner> banners = this.engSystemBannerMapper.selectList(new EntityWrapper<>(EngSysBanner.builder().status(Constant.status_ok).type(2).build()));
        return ServerResponse.ok(banners.stream().map(EngSysBanner::getUrl).collect(Collectors.toList()));
    }

}
